package com.tdrc.common.service;

import com.tdrc.common.beans.Menu;
import com.tdrc.common.mapper.MenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dpf
 * @version 1.0
 * @date 2020-4-7 15:36
 * @instruction ...
 */
@Service
public class MenuService implements IMenuService {
    @Resource
    private MenuMapper menuMapper;
    @Override
    public void addMenu(Menu menu) {
        menuMapper.addMenu(menu);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateMenu(menu);

    }

    @Override
    public void deleteMenu(Integer id) {
        menuMapper.deleteMenu(id);
    }

    @Override
    public List<Menu> menuNav(Integer userId) {
        return menuMapper.menuNav(userId);
    }

    /**
     * 递归生成树形菜单
     * @return
     */
    @Override
    public List<Menu> treeMenu(Integer id,String menuName) {
        return this.getTreeMenu(id,menuName);
    }

    @Override
    public List<Map<String, Object>> listMMenu() {
        return menuMapper.listMMenu();
    }

    public List<Menu> getTreeMenu(Integer id,String menuName){
        List<Menu> treeMenu = new ArrayList<Menu>();
        List<Menu> childrenList = menuMapper.getChildrenMenu(id,menuName);
            for(Menu menu : childrenList){
                if(!this.getTreeMenu(menu.getId(),menuName).isEmpty()){
                    menu.setChildren(this.getTreeMenu(menu.getId(),menuName));
                }
                treeMenu.add(menu);
            }
        return treeMenu;
     }
}
